package com.wxw.BaseLibrary.component;

import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import com.wxw.BaseLibrary.element.IDataGridCellViewAgent;
import lombok.Getter;
import lombok.Setter;

import java.text.DecimalFormat;

/**
 * Created by david on 2014-06-05.
 */
public class DataGridCell {
//    protected TextView cellTextView;
    @Setter
    @Getter
    protected View cellView;
    @Getter
    protected int col = -1;
    @Getter
    protected int row = -1;
    @Setter
    @Getter
    protected int bgColor = Color.WHITE;
    @Setter
    @Getter
    protected int fontColor = Color.BLACK;
    @Setter
    @Getter
    protected float textSize = 14f;
    @Setter
    @Getter
    protected int spanCols = 1;
    @Setter
    @Getter
    protected int textGravity = Gravity.CENTER;
    @Setter
    @Getter
    protected DataGridColumnType columnType = DataGridColumnType.TextViewCell;
    protected String value = "";
    protected Object objValue = null;
    @Setter
    @Getter
    protected String mapping = "";
    @Setter
    @Getter
    protected String float_format = "";
    @Setter
    @Getter
    protected int width = 0;
    @Setter
    @Getter
    protected int height = 50;
    protected boolean isChanged = false;
    protected boolean canClick = true;
    @Getter
    @Setter
    protected int styleId = -1;
    protected DataGridViewRow parent = null;

    public boolean isChanged() {
        return isChanged;
    }

    public void setChanged(boolean isChanged) {
        this.isChanged = isChanged;
    }


    public boolean isCanClick() {
        return canClick;
    }

    public void setCanClick(boolean canClick) {
        this.canClick = canClick;
    }

    public String getValue() {
        if (!"".equals(float_format) && com.wxw.BaseLibrary.tool.Math.isFloatString(value)){
            float f = Float.parseFloat(value);
            DecimalFormat decimalFormat = new DecimalFormat(float_format);
            return decimalFormat.format(f);
        }
        return value;
    }

    public void setValue(String value) {
        isChanged = true;
        this.value = value;
        if (objValue == null) {
            objValue = value;
        }
//        if (cellTextView != null)cellTextView.setText(getValue());
        if (cellView != null) {
            if (cellView instanceof TextView) {
                ((TextView)cellView).setText(getValue());
            } else {
                IDataGridCellViewAgent iDataGridCellViewAgent = parent.getParent().getHeads().getCell(col).getIDataGridCellViewAgent();
                if (iDataGridCellViewAgent != null) {
                    parent.getParent().getHeads().getCell(col).getIDataGridCellViewAgent().setViewContent(this, cellView);
                }
            }
        }
    }

    public Object getObjValue() {
        return objValue;
    }

    public void setObjValue(Object objValue) {
        this.objValue = objValue;
        if ("".equals(value)) {
            value = objValue.toString();
        }
    }

    public DataGridViewRow getParent(){
        return this.parent;
    }

    public void refreshCellView() {
        if (cellView != null && cellView instanceof TextView) {
            ((TextView)cellView).setTextColor(fontColor);
        }
    }

    public DataGridCell(DataGridViewRow parent,int col, int row, String text, Object objValue, int bgColor){
        this.parent = parent;
        this.col = col;
        this.row = row;
        this.value = text;
        this.objValue = objValue;
        this.bgColor = bgColor;
        this.height = parent.getParent().get_RowHeight();
        this.textSize = parent.getParent().get_fontSize();
        this.fontColor = parent.getParent().get_fontColor();
    }
}

